package com.yehaikeji.workflow.controller.management;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehaikeji.workflow.bean.Model;
import com.yehaikeji.workflow.bean.Receive;
import com.yehaikeji.workflow.bean.user;
import com.yehaikeji.workflow.controller.bean.ResultMessage;
import com.yehaikeji.workflow.controller.bean.resultlist;
import com.yehaikeji.workflow.service.ModelService;
import com.yehaikeji.workflow.service.SmsOperationService;

import io.swagger.annotations.ApiOperation;

@RequestMapping("noteModel")
@Controller
public class notemodelController {
	@Autowired
	private ModelService modelService;
	
	@Autowired
	private SmsOperationService smsOperationService;
	
	@RequestMapping(value = "/manage")
	private String manageradmin(ModelMap map) {
		map.put("ismenu",1);
		return "noteModel";
	}
	
	@RequestMapping(value = "/receive")
	@ResponseBody
	private void receive(Receive receive) {
		System.out.println("=====回调数据：" + receive);
		if("mo".equals(receive.getEvents())){//短信上行
			smsOperationService.addReceiveSms(receive);
		}else if("delivered".equals(receive.getEvents())){//短信发送成功
			smsOperationService.modifyStatusBySendId(receive.getSend_id(), "1", "回调接口显示发送成功");
		}
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "新增短信")
	private ResultMessage add(@RequestParam String name, @RequestParam String content,HttpSession httpSession) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if(null == httpSession || null == httpSession.getAttribute("user")){
				resultMessage.setMessage("当前用户未登陆");
				resultMessage.setSuccess(false);
			}else{
				user user = (com.yehaikeji.workflow.bean.user) httpSession.getAttribute("user");
				resultMessage.setData(modelService.addModel(new Model(user.getUserid(),name,content)));
				resultMessage.setSuccess(true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}
	
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "修改短信")
	private ResultMessage modify(@RequestParam String id,@RequestParam String name, @RequestParam String content,HttpSession httpSession) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if(null == httpSession || null == httpSession.getAttribute("user")){
				resultMessage.setMessage("当前用户未登陆");
				resultMessage.setSuccess(false);
			}else{
				Model model = new Model();
				model.setId(id);
				model.setName(name);
				model.setContent(content);
				resultMessage.setData(modelService.modifyModel(model));
				resultMessage.setSuccess(true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = "删除短信")
	private ResultMessage delete(@RequestParam String id,HttpSession httpSession) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if(null == httpSession || null == httpSession.getAttribute("user")){
				resultMessage.setMessage("当前用户未登陆");
				resultMessage.setSuccess(false);
			}else{
				int result = modelService.deleteModel(id);
				if(result > 0){
					resultMessage.setMessage("删除数据成功");
					resultMessage.setSuccess(true);
				}else{
					resultMessage.setMessage("未找到删除的数据");
					resultMessage.setSuccess(false);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "短信列表", notes = "分页按名称搜索")
	private resultlist userlistpagelist(@RequestParam Integer page, @RequestParam Integer limit,
			@RequestParam(required = false) String name) {
		resultlist resultMessage = new resultlist();
		try {
			if (page == null || page <= 0 || limit == null || limit <= 0) {
				resultMessage.setCode(1);
				resultMessage.setMsg("参数错误");
			} else {
				PageHelper.startPage(page, limit);
				List<Model>  list = modelService.listPageModels(name);
				PageInfo<Model> pageInfo = new PageInfo<>(list);
				resultMessage.setData(pageInfo.getList());
				resultMessage.setCount(pageInfo.getTotal());
				resultMessage.setCode(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}
}
